%%--------------------------------------------------------------------
%% Author: <vjache>
%% Created: 17.03.2009
%% Description: TODO: Add description to merge_sort
%%--------------------------------------------------------------------
-module(iter_range).

%%--------------------------------------------------------------------
%% Include files
%%--------------------------------------------------------------------
-include("../include/iter.hrl").
%%--------------------------------------------------------------------
%% Exported Functions
%%--------------------------------------------------------------------
-export([
		 new/2,
		 new/4,
		 open/1,
		 next/1,
		 close/1,
		 test/0
		 ]).

%%--------------------------------------------------------------------
%% API Functions
%%--------------------------------------------------------------------
new(N,Fun) when is_function(Fun, 1),is_integer(N) ->
	iter:from_module(?MODULE, {0,N,1,Fun}).
new(Start,Finish,Step,Fun) when is_function(Fun, 1),
								is_number(Start),is_number(Finish),is_number(Step) ->
	iter:from_module(?MODULE, {Start,Finish,Step,Fun}).
open({Start,Finish,Step,Fun}) ->
	{Start,Finish,Step,Fun}.

next(S={Start,Finish,_Step,_Fun}) when Start>=Finish ->
	{S,?EOF};
next({Start,Finish,Step,Fun}) ->
	{{Start+Step,Finish,Step,Fun},Fun(Start)}.

close(_S) ->
	ok.

%%--------------------------------------------------------------------
%% Test Functions
%%--------------------------------------------------------------------

test() ->
	iter:print(new(10,fun(N)->
							  N*2 
				   end)).

%%--------------------------------------------------------------------
%% Local Functions
%%--------------------------------------------------------------------